<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
class Admin extends Controller
{
    //查询管理员并分页
    public function index(){
        if(!empty($_GET['adminname'])){
            $name = $_GET['adminname'];
            $result = Db::name('admin')
               ->where('adminname',$name)
               ->paginate(1);
           $num = count(Db::name('admin')
               ->where('adminname',$name)
               ->select());
        }else{
            $num = count(Db::name('admin')
                ->select());
            $result = Db::name('admin')
            ->order('adminid desc')
                ->paginate(5);
        }
        $this->assign("result",$result);
        $this->assign("num",$num);
        return $this->fetch('adminlist/adminlist');
    }
    //删除管理员
    public function delectadmin(){
        $adminid = (int)$_GET['adminid'];
        $res = Db::name('admin')->where('adminid',$adminid)->delete();
        if($res>0){
            $this->redirect('http://118.24.83.96:80/wojuAdmin/public/admin/Admin/index');
        }
    }
    //渲染管理员页面
    public function addadminfet(){
        return $this->fetch('adminlist/admin-add');
    }
    //添加管理员
    public function addadmin(){
        if(!empty($_POST['adminName']) && !empty($_POST['password'])){
            $adminname = $_POST['adminName'];
            $adminpwd = $_POST['password'];
            $adminicon = "0";
            $data = [
                'adminname' => $adminname,
                'adminpwd' => md5($adminpwd),
                'adminicon' => $adminicon
            ];
            $res = Db::name('admin')->insert($data);
            if($res>0){
                $this->redirect('http://118.24.83.96:80/wojuAdmin/public/admin/Admin/index');
            }
        }
    }
    //渲染修改页面
    public  function upadminfet(){
        return $this->fetch('adminlist/admin-up');
    }
    //修改管理员密码
    public function upadmin(){
        if(!empty($_POST['password'])){
            $adminid= Session::get('login_admin');
            $adminpwd = $_POST['password'];
            $res = Db::name('admin')->where('adminid',$adminid)->update(['adminpwd'=>md5($adminpwd)]);
            if($res>0){
                $this->redirect('http://118.24.83.96:80/wojuAdmin/public/admin/Admin/index');
            }
        }
    }
}

?>